<template>
	<view class="login-container">
		<view class="logo-container">
			<image src="/static/petLogo.png" class="login-logo" mode="aspectFit"></image>
		</view>
		<view class="login-card">
			<view class="title">登录</view>
			<view class="login-tabs">
				<view class="tab" :class="{ active: loginType === 'password' }" @click="loginType = 'password'">密码登录
				</view>
				<view class="tab" :class="{ active: loginType === 'code' }" @click="loginType = 'code'">验证码登录</view>
			</view>
			<form>
				<view class="form-item">
					<label>手机号</label>
					<input v-model="form.phone" placeholder="请输入手机号" />
				</view>
				<!-- 密码登录表单项 -->
				<view class="form-item" v-if="loginType === 'password'">
					<label>密码</label>
					<input v-model="form.password" type="password" placeholder="请输入密码" show-password />
				</view>
				<!-- 验证码登录表单项 -->
				<view class="form-item verify-code-item" v-if="loginType === 'code'">
					<label>验证码</label>
					<view class="code-input-container">
						<input v-model="form.code" placeholder="请输入验证码" />
						<button class="code-btn" @click.prevent="getVerifyCode" :disabled="countdown > 0">
							{{ countdown > 0 ? `${countdown}秒后重试` : '获取验证码' }}
						</button>
					</view>
				</view>
				<!-- 隐私协议勾选 -->
				<view class="policy-container">
					<view class="checkbox-container" @click="agreePolicy = !agreePolicy">
						<view class="checkbox" :class="{ checked: agreePolicy }"></view>
						<text>我已阅读并同意</text>
						<text class="link-text" @click="goPrivacy">《隐私政策和用户协议》</text>
					</view>
				</view>
				<button v-if="loginType === 'code'" type="submit" @click="verifyLogin" class="sign-up-btn">登录</button>
				<button v-else type="submit" @click="login" class="sign-up-btn">登录</button>
			</form>
		</view>
	</view>
</template>

<script>
	import {
		navigateTo
	} from '@dcloudio/uni-app';
	import {
		userApi
	} from '@/api/user.js'

	export default {
		data() {
			return {
				globaApi: '',
				agreePolicy: false,
				loginType: 'password',
				// 倒计时定时器
				timer: null,
				// 倒计时秒数
				countdown: 0,
				form: {
					phone: '',
					password: '',
					code: ''
				}
			};
		},
		onLoad() {

		},
		methods: {
			// 验证手机号
			validatePhone() {
				if (!this.form.phone || this.form.phone.length !== 11) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return false;
				}
				return true;
			},
			// 获取验证码
			async getVerifyCode() {

				if (!this.validatePhone()) return;
				if (this.countdown > 0) return;

				try {
					uni.showToast({
						title: '发送中...'
					})
					const result = await userApi.getVerifyCode({
						phone: this.form.phone
					})
					// 开始倒计时
					this.countdown = 60;
					this.timer = setInterval(() => {
						this.countdown--;
						if (this.countdown <= 0) {
							clearInterval(this.timer);
						}
					}, 1000);
					uni.showToast({
						title: '发送成功'
					})
					console.log('获取验证码成功', result);
				} catch (error) {
					uni.showToast({
						title: '获取验证码失败: ' + (error.message || '未知错误'),
						icon: 'none'
					});
					console.error('获取验证码失败', error);
				}
			},
			async login() {
				console.log('密码');
				if (!this.form.phone || !this.form.phone.trim()) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				if (!this.form.password || !this.form.password.trim()) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return;
				}
				if (!this.agreePolicy) {
					uni.showToast({
						title: '请阅读并同意隐私政策',
						icon: 'none'
					});
					return;
				}

				try {
					uni.showLoading({
						title: '登录中...'
					});

					const result = await userApi.login({
						phone: this.form.phone,
						password: this.form.password
					})
					uni.setStorageSync('token', result.token);
					uni.setStorageSync('username', result.username);
					uni.setStorageSync('userId', result.userId);
					uni.setStorageSync('avatar', result.avatar);
					uni.setStorageSync('sex', result.sex);
					uni.setStorageSync('birthday', result.birthday);
					uni.setStorageSync('introduction', result.introduce);
					uni.setStorageSync('telephone', result.phone);
					uni.showToast({
						title: '登录成功'
					});
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						})
					})
					console.log('登录成功', result)
				} catch (error) {
					uni.showToast({
						title: '登录失败: ' + (error.message || '未知错误'),
						icon: 'none'
					});
					console.error('登录失败：', error);
				}
			},
			async verifyLogin() {

				if (!this.form.phone || !this.form.phone.trim()) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				if (!this.form.code || !this.form.code.trim()) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return;
				}
				if (!this.agreePolicy) {
					uni.showToast({
						title: '请阅读并同意隐私政策',
						icon: 'none'
					});
					return;
				}

				try {
					uni.showToast({
						title: '登录中...'
					});

					const result = await userApi.verifyLogin({
						phone: this.form.phone,
						code: this.form.code
					})
					uni.setStorageSync('token', result.token);
					uni.setStorageSync('username', result.username);
					uni.setStorageSync('userId', result.userId);
					uni.setStorageSync('avatar', result.avatar);
					uni.setStorageSync('sex', result.sex);
					uni.setStorageSync('birthday', result.birthday);
					uni.setStorageSync('introduction', result.introduce);
					uni.setStorageSync('telephone', result.phone);
					uni.showToast({
						title: '登录成功'
					});
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/index/index'
						})
					})
					console.log('登录成功', result)
				} catch (error) {
					uni.showToast({
						title: '登录失败: ' + (error.message || '未知错误'),
						icon: 'none'
					});
					console.error('登录失败：', error);
				}
			},
			goPrivacy() {
				uni.navigateTo({
					url: '/pages/ordinance/privacy'
				})
			},
			goAgreement() {
				uni.navigateTo({
					url: '/pages/ordinance/agreement'
				})
			},
		}
	};
</script>

<style scoped>
	.login-container {
		background: linear-gradient(135deg, #F9CB43 0%, #FBA518 100%);
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.login-card {
		background: white;
		padding: 40rpx;
		border-radius: 30rpx;
		width: 80%;
		max-width: 900rpx;
		margin-bottom: 200rpx;
	}

	.logo-container {
		text-align: center;
		margin-bottom: 100rpx;
	}

	.login-logo {
		width: 240rpx;
		height: 240rpx;
		display: block;
		margin: 0 auto;
		border-radius: 30rpx;
	}

	.title {
		text-align: center;
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 40rpx;
		color: #333;
	}

	.form-item {
		margin-bottom: 30rpx;
	}

	label {
		display: block;
		margin-bottom: 15rpx;
		color: #666;
		font-size: 28rpx;
	}

	input {
		width: 100%;
		height: 50px;
		padding: 0px 5px;
		border: 2rpx solid #e0e0e0;
		border-radius: 20rpx;
		font-size: 28rpx;
	}

	.sign-up-btn {
		width: 100%;
		padding: 25rpx;
		background: #FBA518;
		color: white;
		border: none;
		border-radius: 20rpx;
		font-size: 30rpx;
		margin-bottom: 30rpx;
	}

	.sign-in-link {
		text-align: center;
		font-size: 28rpx;
		color: #666;
	}

	.link-text {
		color: #FBA518;
		text-decoration: underline;
	}

	.login-tabs {
		display: flex;
		margin-bottom: 30rpx;
		border-bottom: 1rpx solid #e0e0e0;
	}

	.tab {
		flex: 1;
		text-align: center;
		padding: 20rpx 0;
		font-size: 28rpx;
		color: #666;
		position: relative;
	}

	.tab.active {
		color: #FBA518;
	}

	.tab.active:after {
		content: '';
		position: absolute;
		bottom: -2rpx;
		left: 25%;
		width: 50%;
		height: 4rpx;
		background: #FBA518;
		border-radius: 2rpx;
	}

	/* 验证码输入框样式 */
	.verify-code-item {
		position: relative;
	}

	.code-input-container {
		display: flex;
		align-items: center;
	}

	.code-input-container input {
		flex: 1;
		margin-right: 20rpx;
	}

	.code-btn {
		width: 200rpx;
		height: 70rpx;
		line-height: 70rpx;
		background: #FBA518;
		color: white;
		font-size: 24rpx;
		padding: 0;
		border-radius: 20rpx;
	}

	.code-btn[disabled] {
		background: #e0e0e0;
		color: #999;
	}

	/* 隐私协议勾选样式 */
	.policy-container {
		margin-bottom: 30rpx;
	}

	.checkbox-container {
		display: flex;
		align-items: center;
		font-size: 24rpx;
		color: #666;
	}

	.checkbox {
		width: 32rpx;
		height: 32rpx;
		border: 2rpx solid #e0e0e0;
		border-radius: 6rpx;
		margin-right: 10rpx;
		position: relative;
	}

	.checkbox.checked {
		background: #FBA518;
		border-color: #FBA518;
	}

	.checkbox.checked:after {
		content: '✓';
		position: absolute;
		color: white;
		font-size: 24rpx;
		top: 0;
		left: 6rpx;
	}

	.link-text {
		color: #FBA518;
		margin: 0 6rpx;
	}
</style>